package com.example.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.operation.domin.DecWaresClassify;
import com.example.operation.dto.DecWaresClassifyDto;
import com.example.operation.mapper.DecWaresClassifyMapper;
import com.example.operation.service.DecWaresClassifyService;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class DecWaresClassifyServiceImpl extends ServiceImpl<DecWaresClassifyMapper, DecWaresClassify> implements DecWaresClassifyService {

    @Autowired
    private DecWaresClassifyMapper decWaresClassifyMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(DecWaresClassifyDto decWaresClassifyDto) {
        DecWaresClassify decWaresClassify = new DecWaresClassify();
        LambdaQueryWrapper<DecWaresClassify> eq = new QueryWrapper<DecWaresClassify>()
                .lambda().eq(DecWaresClassify::getName,decWaresClassifyDto.getName());
        Integer count = decWaresClassifyMapper.selectCount(eq);
        if(count > 0){
            throw new RuntimeException("分类名称不能重复");
        }
        BeanUtils.copyProperties(decWaresClassifyDto,decWaresClassify);
        decWaresClassify.setCreateTime(LocalDateTime.now());
        decWaresClassify.setModifyTime(LocalDateTime.now());
        decWaresClassify.setLabel(decWaresClassifyDto.getLabel().toString());
        decWaresClassifyMapper.insert(decWaresClassify);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer update(DecWaresClassifyDto decWaresClassifyDto) {
        DecWaresClassify decWaresClassify = new DecWaresClassify();
        LambdaUpdateWrapper<DecWaresClassify> eq = new UpdateWrapper<DecWaresClassify>()
                .lambda().eq(DecWaresClassify::getName,decWaresClassifyDto.getName());
        BeanUtils.copyProperties(decWaresClassifyDto,decWaresClassify);
        decWaresClassify.setLabel(decWaresClassifyDto.getLabel().toString());
        decWaresClassify.setModifyTime(LocalDateTime.now());
        return decWaresClassifyMapper.update(decWaresClassify,eq);
    }

    /**
     * 分页查询
     * @param pageCurrent
     * @param pageSize
     * @param pageNumber
     * @param name
     * @return
     */
    @Override
    public Page<DecWaresClassify> page(Integer pageCurrent, Integer pageSize, Integer pageNumber, String name) {
        Page<DecWaresClassify>page = new Page<>(pageCurrent,pageSize, pageNumber);
        LambdaQueryWrapper<DecWaresClassify> eq = new QueryWrapper<DecWaresClassify>()
                .lambda().eq(DecWaresClassify::getState,1)
                .like(StrUtil.isNotBlank(name),DecWaresClassify::getName,name);
        return decWaresClassifyMapper.selectPage(page,eq);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer delectById(Integer id) {
        return decWaresClassifyMapper.deleteById(id);
    }

    @Override
    public DecWaresClassify selectById(Integer id) {
        return decWaresClassifyMapper.selectById(id);
    }
}
